/**
 * Created by gongxun on 17/5/26.
 */

//上楼
var KwxTableShanglouLayer = cc.Layer.extend({
    mDiceLeft : null,
    mDiceRight : null,

    mIsActivation : null,

    //上楼文字
    mText_shanglou : null,

    //上楼定位
    mLocationNode : null,
    ctor : function(){
        this._super();

        this.mIsActivation = false;
    },

    onEnter : function(){
        this._super();

    },

    //如果是上楼规则 就激活
    onActivation : function(){
        this.mIsActivation = true;

        ccs.armatureDataManager.addArmatureFileInfo("res/effect/moveDice.json");

        this.mDiceLeft = new ccs.Armature("WeChat_Lroding");
        this.mDiceLeft.setScale(1);
        this.mDiceLeft.setPosition(mo.gameSize.x / 2, mo.gameSize.y / 2);
        this.mDiceLeft.animation.setSpeedScale(2);
        this.addChild(this.mDiceLeft);

        this.mDiceRight = new ccs.Armature("WeChat_Lroding");
        this.mDiceRight.setScale(1);
        this.mDiceRight.setPosition(mo.gameSize.x / 2, mo.gameSize.y / 2);
        this.mDiceRight.animation.setSpeedScale(2);
        this.addChild(this.mDiceRight);

        this.mDiceLeft.setVisible(false);
        this.mDiceRight.setVisible(false);
    },

    //播放骰子动画
    runDices : function(dice1, dice2){
        if (!this.mIsActivation){
            return;
        }

        cc.log("骰子1 " + dice1);
        cc.log("骰子2 " + dice2);

        this.mDiceLeft.setVisible(true);
        this.mDiceRight.setVisible(true);

        this.mDiceLeft.animation.play("KWX_DiceA" + dice1);
        this.mDiceRight.animation.play("KWX_DiceB" + dice2);

        var that = this;
        this.stopAllActions();
        this.runAction(cc.sequence(
            cc.delayTime(1),
            cc.callFunc(function(){
                that.mDiceLeft.animation.stop();
                that.mDiceRight.animation.stop();

                that.mDiceLeft.setVisible(false);
                that.mDiceRight.setVisible(false);

                var numDice = parseInt(dice1) + parseInt(dice2);
                if ( numDice % 2 == 0){
                    that.runShangLouTextAction();
                }
            })
        ));
    },

    //播放上楼番薯动画
    runShangLouTextAction : function(){
        this.mText_shanglou.stopAllActions();
        this.mText_shanglou.setScale(0.25);
        this.mText_shanglou.setPosition(cc.p(0, -mo.gameSize.y / 2 + 20));

        this.mText_shanglou.runAction(cc.sequence(
            cc.scaleTo(0.2, 1),
            cc.delayTime(0.5),
            cc.spawn(
                cc.scaleTo(0.2, 0.25),
                cc.moveTo(0.2, cc.pAdd(this.mLocationNode.getPosition(), cc.p(0, -25)))
            )
        ));
    },

    //设置上楼需要的位置控件
    setShanglouLocationNode : function(locationNode){
        this.mLocationNode = locationNode;
    }
});